package com.epoint.spring.job;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QuartzController {

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/addJob")
    public String addJob(Long jobId, String jobGroup, String identity, String triggerIdentity) throws SchedulerException {
        Class<? extends Job> jobClass = QuartzDisallowConcurrentExecution.class;
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(identity).build();

        // 表达式调度构建器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");

        // 按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerIdentity)
                .withSchedule(cronScheduleBuilder).build();

        // 放入参数，运行时的方法可以获取
        jobDetail.getJobDataMap().put("ceshi", "测试而已");

        scheduler.scheduleJob(jobDetail, trigger);

        return "添加成功";
    }

}
